package com.mapforge.gui.tools;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import com.mapforge.Constants;
import com.mapforge.map.TileMap;
import com.mapforge.map.tools.ZoomTool;

public class ZoomToolPanel extends JPanel {
	/**
	 * Default eclipse generated ID, here to remove warnings.
	 */
	private static final long serialVersionUID = 1L;

	private ZoomTool zoomTool;
	private JLabel lblCurrentZoom;
	private JToggleButton zoomInBtn, zoomOutBtn;
	private JButton zoomNormal;
	private TileMap tileMap;

	public ZoomToolPanel(ZoomTool zoomTool) {
		this.zoomTool = zoomTool;
		ZoomListener listener = new ZoomListener();
		ButtonGroup btnGroup = new ButtonGroup();
		zoomInBtn = new JToggleButton(Constants.ZOOM_IN_ICON);
		zoomOutBtn = new JToggleButton(Constants.ZOOM_OUT_ICON);
		zoomOutBtn.addActionListener(listener);
		zoomInBtn.addActionListener(listener);
		btnGroup.add(zoomOutBtn);
		btnGroup.add(zoomInBtn);
		zoomInBtn.setSelected(true);
		lblCurrentZoom = new JLabel() {
			private static final long serialVersionUID = 1L;

			public String toString() {
				return "%"
						+ String.format("%d3", "" + (tileMap.getScale() * 100));
			}
		};
		add(zoomInBtn);
		add(zoomOutBtn);
		add(lblCurrentZoom);
	}

	public void setTileMap(TileMap tileMap) {
		this.tileMap = tileMap;
		lblCurrentZoom.setText("%" + (this.tileMap.getScale() * 100));
	}

	private class ZoomListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == zoomOutBtn) {
				zoomTool.setDemagnify();
			} else {
				zoomTool.setMagnify();
			}
		}
	}
}